#include "mainwindow.h"
#include "mydialog.h"
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
#include <QWidget>

MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
{
    QWidget *central = new QWidget(this);
    setCentralWidget(central);

    openDialogButton = new QPushButton("打开对话框", this);
    displayLabel = new QLabel("等待输入...", this);

    QVBoxLayout *layout = new QVBoxLayout(central);
    layout->addWidget(openDialogButton);
    layout->addWidget(displayLabel);

    connect(openDialogButton, &QPushButton::clicked, this, &MainWindow::openDialog);
}

void MainWindow::openDialog()
{
    dialog = new MyDialog(this);
    // 连接对话框的信号到主窗口的槽
    connect(dialog, &MyDialog::sendData, this, &MainWindow::receiveData);
    dialog->exec(); // 以模态方式显示对话框
}

void MainWindow::receiveData(const QString &data)
{
    displayLabel->setText("收到数据：" + data);
}
